import { axios } from '@/utils/request'

const precessEnv = process.env
let preUrl = ''
if (precessEnv.VUE_APP_FLAG === 'dev' || precessEnv.VUE_APP_FLAG === 'local') {
  preUrl = ''
} else {
  preUrl = '/user'
}

// 获取用户信息
export const getUserInfo = () => {
  return axios.get(`${preUrl}/ucenter/uuser/loginUserAuthInfo`)
}

export const getUserSceneMenuInfo = () => {
  return axios.get(`${preUrl}/ucenter/userscene/sceneUserMenuInfo`)
}

// 登出
export const logout = () => {
  return axios({ url: `${preUrl}/ucenter/logout`, method: 'delete' })
}
// // 登录 开发环境时使用
// export const login = (username, password, code, randomStr) => {
//   const grant_type = 'password'
//   return axios({
//     url: '/auth/oauth/token',
//     headers: {
//       'tenant-id': 1,
//       Authorization: 'Basic cGlnOnBpZw=='
//     },
//     method: 'post',
//     params: { username, password, randomStr, code, grant_type, scope: 'server' }
//   })
// }

export const preLogin = params => {
  return axios({ url: `${preUrl}/ucenter/preLogin/${params}`, method: 'get' })
}

export const login = data => {
  return axios({ url: `${preUrl}/ucenter/loginWithCode`, method: 'post', data })
}

export const getVerifyCode = params => {
  return axios({ url: `${preUrl}/ucenter/verifyCode`, method: 'get', params, responseType: 'blob' })
}

// 用户密码修改
export const changePassword = params => {
  return axios({ url: `${preUrl}/ucenter/uuser/changePassword`, method: 'post', data: params })
}

// 登录 预发布环境和生产环境使用
export const mobileLogin = (username, password, randomStr) => {
  const grantType = 'password'
  return axios({
    url: '/auth/oauth/token',
    headers: {
      'tenant-id': 1,
      Authorization: 'Basic dGVzdDp0ZXN0'
    },
    method: 'post',
    params: { username, password, randomStr, grantType, scope: 'server' }
  })
}

// 登录----------------------------------------------------------------
// 获取短信验证码
export const getShortCode = params => {
  return axios({ url: '/admin/loginMsn/sendMsn', method: 'get', params })
}

// 验证短信验证码
export const validShortCode = params => {
  return axios({ url: '/admin/loginMsn/checkCode', method: 'get', params })
}

// 忘记密码中的----------------------------------------------------------------
// 验证 手机号
export const validPhoneByForget = mobile => {
  return axios({ url: '/admin/forgetPassword/validation/' + mobile, method: 'get' })
}

// 获取短信验证码
export const getShortCodeByForget = mobile => {
  return axios({ url: '/admin/forgetPassword/sendMessage/' + mobile, method: 'get' })
}

// 验证 短信验证码
export const validShortCodeByForget = params => {
  return axios({ url: '/admin/forgetPassword/validation/checkCode', method: 'get', params })
}

// 保存新密码
export const savePasswordByForget = data => {
  return axios({ url: '/admin/forgetPassword/revertPassword', method: 'put', data })
}

// 修改密码中的----------------------------------------------------------------
// 验证 手机号
export const getShortCodeByEdit = mobile => {
  return axios({ url: '/admin/user/sendMsn/' + mobile, method: 'get' })
}

// 验证 短信验证码
export const validShortCodeByEdit = params => {
  return axios({ url: '/admin/user/checkCode', method: 'get', params })
}

// 修改密码
export const savePasswordByEdit = data => {
  return axios({ url: '/admin/user/edit', method: 'put', data })
}

// 查询手机号
export const findPhone = params => {
  return axios({ url: '/admin/loginMsn/findPhone', method: 'get', params })
}

// 查询租户的角色列表（租户编号不传查默认登录人租户）
export const getAllRoleList = params => {
  return axios({ url: '/alarm/alarm/user/allRoleList', method: 'get', data: params })
}

// 根据角色编号获取角色用户列表
export const getListByRoleNo = params => {
  return axios({ url: '/alarm/user/getListByRoleNo', method: 'get', data: params })
}
